﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Data;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Imaging;
using System.Windows.Navigation;
using System.Windows.Shapes;
using UTC_Project_Manager.Model.Infrastructure;

namespace UTC_Project_Manager.Controls
{
    /// <summary>
    /// Interaction logic for ReunionListControl.xaml
    /// </summary>
    public partial class ReunionListControl : UserControl
    {
        public delegate void DeleteReunionEvent(Meeting meeting);
        public static event DeleteReunionEvent deleteReunionEvent;

        public delegate void ReunionEvent();
        public static event ReunionEvent newReunionEvent;

        public delegate void ReunionSelectedEvent(Meeting meeting);
        public static event ReunionSelectedEvent newReunionSelectedEvent;

        public ReunionListControl()
        {
            InitializeComponent();
            PopulateLists();
            NewReunionControl.newReunionCreatedEvent += new NewReunionControl.NewReunionEvent(NewReunionControl_newReunionCreatedEvent);
            CurrentMeetingControl.newReunionCreatedEvent += new CurrentMeetingControl.NewReunionEvent(NewReunionControl_newReunionCreatedEvent);
        }

        private void  NewReunionControl_newReunionCreatedEvent(Meeting meeting)
        {
            if (this.futureMeetingsListBox.SelectedIndex != -1)
                App.MyConfig.OurProject.Meetings.Remove(this.futureMeetingsListBox.SelectedItem as Meeting);
            if (this.currentMeetingsListBox.SelectedIndex != -1)
                App.MyConfig.OurProject.Meetings.Remove(this.currentMeetingsListBox.SelectedItem as Meeting);
            PopulateLists();
        }
 
        private void PopulateLists()
        {
            this.finishedMeetingsListBox.Items.Clear();
            this.currentMeetingsListBox.Items.Clear();
            this.futureMeetingsListBox.Items.Clear();
            this.removeReunionButton.IsEnabled = false;
            App.MyConfig.OurProject.Meetings.Sort(CompareMeetings);
            foreach (Meeting meeting in App.MyConfig.OurProject.Meetings)
            {

                if (meeting.IsOver)
                    this.finishedMeetingsListBox.Items.Add(meeting);
                else
                {
                    if (TimeDifference(meeting.Date) < 7)
                        this.currentMeetingsListBox.Items.Add(meeting);
                    else
                        this.futureMeetingsListBox.Items.Add(meeting);
                }
            }
        }

        private int TimeDifference(DateTime date)
        {
            return ((TimeSpan)(date - DateTime.Today)).Days;
        }

        private int CompareMeetings(Meeting x, Meeting y)
        {
            if (x == null)
            {
                if (y == null)
                {
                    return 0;
                }
                else
                {
                    return -1;
                }
            }
            else
            {
               if (y == null)
                 return 1;
           
                else
                {
                   return x.Date.CompareTo(y.Date);
                   
                }
            }
        }

        private void addReunionButton_Click(object sender, RoutedEventArgs e)
        {
            this.finishedMeetingsListBox.SelectedIndex = -1;
            this.currentMeetingsListBox.SelectedIndex = -1;
            this.futureMeetingsListBox.SelectedIndex = -1;
            this.removeReunionButton.IsEnabled = false;
            newReunionEvent();
        }

        private void removeReunionButton_Click(object sender, RoutedEventArgs e)
        {
            if (this.futureMeetingsListBox.SelectedIndex != -1){
                App.MyConfig.OurProject.Meetings.Remove(this.futureMeetingsListBox.SelectedItem as Meeting);
                deleteReunionEvent(this.futureMeetingsListBox.SelectedItem as Meeting);
            }
            if (this.currentMeetingsListBox.SelectedIndex != -1)
            {
                App.MyConfig.OurProject.Meetings.Remove(this.currentMeetingsListBox.SelectedItem as Meeting);
                deleteReunionEvent(this.currentMeetingsListBox.SelectedItem as Meeting);
            }
            PopulateLists();
            
        }

        private void futureMeetingsListBox_SelectionChanged(object sender, SelectionChangedEventArgs e)
        {
            if (this.futureMeetingsListBox.SelectedIndex != -1)
            {
                this.finishedMeetingsListBox.SelectedIndex = -1;
                this.currentMeetingsListBox.SelectedIndex = -1;
                this.removeReunionButton.IsEnabled = true;
                newReunionSelectedEvent(this.futureMeetingsListBox.SelectedItem as Meeting);
            }
        }

        private void currentMeetingsListBox_SelectionChanged(object sender, SelectionChangedEventArgs e)
        {
            if (this.currentMeetingsListBox.SelectedIndex != -1)
            {
                this.finishedMeetingsListBox.SelectedIndex = -1;
                this.futureMeetingsListBox.SelectedIndex = -1;
                this.removeReunionButton.IsEnabled = true;
                newReunionSelectedEvent(this.currentMeetingsListBox.SelectedItem as Meeting);
            }
        }

        private void finishedMeetingsListBox_SelectionChanged(object sender, SelectionChangedEventArgs e)
        {
            if (this.finishedMeetingsListBox.SelectedIndex != -1)
            {
                this.removeReunionButton.IsEnabled = false;
                this.currentMeetingsListBox.SelectedIndex = -1;
                this.futureMeetingsListBox.SelectedIndex = -1;
                newReunionSelectedEvent(this.finishedMeetingsListBox.SelectedItem as Meeting);

            }
        }
    }
}
